<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'edit', 'quickSearch', 'comSearch']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('fp.post.audit.quick Search Fields') })"
        ></TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">

            <template #customColmn>
                <!-- 还可以继续使用 el-table-column 组件本身的插槽 -->
                <el-table-column prop="post.content" :label="t('fp.post.audit.post_content')" width="200">
                    <template #default="scope">
                        <div class="text-ellipsis" :title="scope.row.post.content">{{ scope.row.post.content }}</div>
                    </template>
                </el-table-column>
                <!-- 还可以继续使用 el-table-column 组件本身的插槽 -->
                <el-table-column prop="video_cover_url_bak" label="截图预览" width="180">
                    <template #default="scope">
                        <el-image
                            v-if="scope.row.media_type === 'video' && scope.row.video_cover_url_bak[0]"
                            style="max-width: 100px; max-height: 100px"
                            :src="scope.row.video_cover_url_bak[0]"
                            :preview-src-list="scope.row.video_cover_url_bak"
                            :zoom-rate="1.2"
                            :max-scale="7"
                            :min-scale="0.2"
                            :z-index="99999999"
                            :preview-teleported=true
                            fit="cover"
                        />
                    </template>
                </el-table-column>
                <el-table-column prop="video_cover_url" label="选中封面" width="180">
                    <template #default="scope">
                        <el-image
                            v-if="scope.row.media_type === 'video' && scope.row.video_cover_url"
                            style="max-width: 100px; max-height: 100px"
                            :src="scope.row.video_cover_url"
                            :preview-src-list="[scope.row.video_cover_url]"
                            :zoom-rate="1.2"
                            :max-scale="7"
                            :min-scale="0.2"
                            :z-index="99999999"
                            :preview-teleported=true
                            fit="cover"
                        />
                    </template>
                </el-table-column>

            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import {onMounted, provide, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import PopupForm from './popupForm.vue'
import {baTableApi} from '/@/api/common'
import {defaultOptButtons} from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'

defineOptions({
    name: 'fp/post/audit',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons([])

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/fp.PostAudit/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('fp.post.audit.id'), prop: 'id', align: 'center', width: 180, operator: false,sortable: false, sortable: 'custom' },
            { label: t('fp.post.audit.status'), prop: 'status', align: 'center',width: 100,  render: 'tag', operator: 'eq', sortable: true, replaceValue: { '0': '待审核', '1': '审核通过', '2': '审核拒绝' } },
            { label: t('fp.post.audit.media_type'), prop: 'media_type', align: 'center', render: 'tag', width: 100, operator: 'eq', sortable: false, replaceValue: { image: t('fp.post.audit.media_type image'), video: t('fp.post.audit.media_type video'), unknown: t('fp.post.audit.media_type unknown') } },
            { label: t('fp.post.audit.post_id'), prop: 'post_id', align: 'center', width: 180,operator: false, sortable: false },
            { label: t('fp.post.audit.title'), prop: 'post.title', align: 'center', width: 180, operator: false, sortable: false },
            {  render: 'slot', slotName: 'customColmn'},
            { label: t('fp.post.audit.audit_user_id'), prop: 'admin.nickname', align: 'center', width: 100, operator: 'like', sortable: false },
            { label: t('fp.post.audit.audit_reason'), prop: 'audit_reason', align: 'center',width: 180,  operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false},
            { label: t('fp.post.audit.create_time'), prop: 'create_time', align: 'center',width: 180,  operator: false, sortable: 'custom', render: 'datetime', timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('fp.post.audit.update_time'), prop: 'update_time', align: 'center',width: 180,  operator: false, sortable: 'custom', render: 'datetime', timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('Operate'), align: 'center', render: 'buttons', buttons: optButtons, operator: false,showOverflowTooltip: true},
        ],
        dblClickNotEditColumn: [undefined],
        defaultOrder: { prop: 'status', order: 'asc' },
        showComSearch:true
    },
    {
        defaultItems: { status: '0' },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">
.text-ellipsis {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    display: block;
    width: 100%; /* 必须指定宽度 */
    cursor: pointer; /* 鼠标悬浮显示手型 */
}
</style>
